yjx.controller('teamController',['$rootScope','$scope','$fetch','modalService','$timeout',function ($rootScope,$scope,$fetch,modalService,$timeout) {
    // $scope.myTeam = '';
    $rootScope.tabCtrlName = $rootScope.tabCtrlNameArr[3];
    $scope.teamTags = ['我组建的团队','我加入的团队'];
    $scope.teamTag = $scope.teamTags[0];
    $scope.applying = false;
    $scope.teamModal = false;
    $scope.teamModalTitle= "请输入团队的名称";
    $scope.teamName= '';
    var msgInfo = "您已成功发出加入申请！<br>申请信息将在我的消息中展示";
    $scope.status='',/*创建加入团队的状态*/
    /*
    * :
     //status: 0 :没有创建团队 也没有加入任何团队
     :
     //status  1 :拥有自己的团队 	没有加入其他团队
     :
     //status: 2 :拥有自己的团队 	 也加入了团队 已是正式成员
     :
     //status:3 :拥有自己的团队 有申请加入其他团队 但还在审批中
     :
     //status: 4 :没有创建团队 但有加入其它团队 已是正式成员
     :
     //status: 5 :没有创建团队 但有申请加入其它团队 正在申请中
    *
    * */

    $scope.createTeam = function () {
        $scope.teamModalTitle= "请输入团队的名称";
        $scope.teamModal = true;
    };
    
    $scope.joinTeam = function () {
        //一个ID同时只能申请一个团队 
        $scope.teamModalTitle= "请输入您想加入的团队ID";
        $scope.teamModal = true;
    }


    /*切换tag*/
    $scope.changeTag = function(i){
        $scope.teamTag = $scope.teamTags[i];
        initTag();
    }
    /*切换tag时的数据初始化*/
   function initTag() {
       $scope.teamModal = false;
   }
    /*获取团队管理信息*/
    function getKpiInfo(){
        $fetch.fetch(
            '/group/kpiInfo',
            {
                params:{
                    userId:$rootScope.userId
                },
                cache:false
                //type:'POST'
            }
        ).then(function(data){
                console.log(data)
                $timeout(function(){
                    $scope.status = data.status;
                    /*自己加入的团队*/
                    $scope.joinedTeam = data.joinGroupInfo;
                    $scope.myData = data.joGroupMemberInfo?data.joGroupMemberInfo[0]:'';
                    ;
                    /*自己创建的团队*/
                    $scope.myTeam = data.groupInfo;
                    $scope.memberList = data.groupMemberInfo;
                    $scope.myTeam.baseInfo = false;
                    if($scope.myTeam.salesVolume || $scope.myTeam.relayContentTimes || $scope.myTeam.memberCount || $scope.myTeam.brokerage) {
                        $scope.myTeam.baseInfo = true;
                    }

                    //if(data.status==1){
                    //    /*加入其它团队 已是正式成员*/
                    //    $scope.joinedTeam = data.groupInfo
                    //    $scope.myData = data.groupMemberInfo[0];
                    //}else if(data.status == 3) {
                    //    /*自己已经创建团队*/
                    //    $scope.memberList = data.groupMemberInfo;
                    //    $scope.myTeam = data.groupInfo;
                    //    $scope.myTeam.baseInfo = false;
                    //    if($scope.myTeam.salesVolume || $scope.myTeam.relayContentTimes || $scope.myTeam.memberCount || $scope.myTeam.brokerage) {
                    //        $scope.myTeam.baseInfo = true;
                    //    }
                    //}
                })
            }).catch(function (e) {

            })
    }
    if($rootScope.userId) {
        getKpiInfo()
    }

//模态框操作
    $scope.teamModalAct = function () {
        console.log($scope.teamName);
        if(!$scope.teamName){
            $scope.teamModal = false;
            return
        }

        if($scope.teamTag == $scope.teamTags[0]){
            //组建团队
            // $scope.myTeam={
            //     teamId:'X123X',
            //     teamName:'地表最强基因销售队',
            //     baseInfo:true,
            //     sales:12345,
            //     salesTop:'nunu',
            //     forwards:123,
            //     forwardsTop:'mimi',
            //     members:12,
            //     brokerages:345,
            // }

            $fetch.fetch(
                '/group/create',
                {
                    params:{
                        userId:$rootScope.userId,
                        name:$scope.teamName
                    },
                    type:'POST'
                }
            ).then(function(data){

                    $timeout(function(){
                        //if(data){
                            $scope.myTeam = data
                            $scope.teamModal = false;
                            $scope.teamName = '';
                            //$scope.status = 3;
                        //}
                    })
                }).catch(function(e){
                    $scope.teamModal = false;
                    $scope.teamName = '';
                    modalService.mainMsgError(e.msg);
                })
        }else {
            $fetch.fetch(
                '/group/join',
                {
                    params:{
                        userId:$rootScope.userId,
                        no:$scope.teamName
                    },
                    type:'POST'
                }
            ).then(function(data){
                    $timeout(function(){
                            modalService.mainMsgInfo('申请信息将在我的消息中展示','','您已成功发出加入申请');
                            $scope.status = 3;
                            $scope.applying = true;
                            $scope.teamModal = false;
                            $scope.teamName = '';
                    })
                }).catch(function(e){
                    $scope.teamModal = false;
                    $scope.teamName = '';
                    modalService.mainMsgError(e.msg);
                })


            //发送入团申请
            //打开信息提示模态框
        }
    }


}])